import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import Home from '../views/Home/index.vue'
import AboutView from '../views/AboutView.vue'
import MessageView from '../views/MessageView.vue'
import Dema from '../views/Home/components/Dema.vue'
import Aiou from '../views/Home/components/Aiou.vue'
import Rose from '../views/Home/components/Rose.vue'
import Login from '../views/Login.vue'
import Text from '../views/Text.vue'
import { createStore } from 'vuex'
// 路由配置文件 router/index.js
// 作用：router是vue中帮助跳转页面的
// vue是单页面文件，看起来是跳转页面，实际上是跳转组件
const routes = [
    {
        path: '/',
        name: 'Home',
        component: Home,
        redirect: '/dema',
        children: [
            {
                path: 'dema',
                name: "Dema",
                component: Dema
            },
            {
                path: 'aiou',
                name: "Aiou",
                component: Aiou
            },
            {
                path: 'rose',
                name: "Rose",
                component: Rose
            },
        ]
    },
    {
        path: '/about/:id',//:id相当于一个占位符
        name: 'AboutView',
        component: AboutView
    },
    {
        path: '/message',
        name: 'MessageView',
        component: MessageView
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/text',
        name: 'Text',
        component: Text,
        // meta:{
        //     auth:true,
        // }
        beforeEnter:(to,from,next)=>{
            
        }
    },
]

const router = createRouter({
    // 该属性决定当前Vue的路由模式是hash模式(createWebHashHistory)，url上带#
    //history模式(createWebHistory)，url上不带#
    history: createWebHistory(),
    routes,
})

router.beforeEach((to,from,next)=>{
    //to 跳转的目的路由
    //from 跳转的离开路由
    //next 跳转的方法
    //完成路由守卫
    // 1.给需要被守卫的路由规则添加元信息meta
    // 2.判断当前进入的路由是否需要（判断是否有meta）
    // 3. 如果需要拦截，判断是否有登录
    if (to.meta.auth) {
        // 当前组件需要拦截
        let isLogin=sessionStorage.getItem('isLogin')
        console.log(isLogin);
        if (!isLogin) {
            // 说明未登录，跳去路由页面
            router.push('/login?redirct='+to.fullPath)
            console.log('未登录');
        }else{
            // 已登录，正常跳转
            next()
        }
    }else{
        // 当前组件不需要拦截
        next()
    }
})

export default router

